<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="Liux">





<title>PostgreSQL的安装与初始化 | 爱羊羊，爱生活。</title>



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


</head>
<body>
    <div class="wrapper ">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo">

<a href="/">卷首</a>

</div>

            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">文章</a>
                
                    <a class="menu-item" href="/category">分类</a>
                
                    <a class="menu-item" href="/tag">标签</a>
                
            </div>

        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">卷首</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">文章</a>
                
                    <a class="menu-item" href="/category">分类</a>
                
                    <a class="menu-item" href="/tag">标签</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>

	<div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">展开</a>
        <a onclick="go_top()">到顶</a>
        <a onclick="go_bottom()">到底</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "收起"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "展开"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>

    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">PostgreSQL的安装与初始化</h1>
            
                <div class="post-meta">
                    
                        <span class="post-time">
                        <a href="#">2020-04-09&nbsp;&nbsp;11:20:00</a>
                        </span>
                    
                    
                </div>
            
        </header>

        <section class="post-tags">
            <div>
                <span class="tag">
                    
                    
                        <a href="/tags/PostgreSQL/"> PostgreSQL</a>
                    
                        <a href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/"> 数据库</a>
                    
                        
                </span>
            </div>
        </section>

        <div class="post-content">
            <h2 id="关于PostgreSQL"><a href="#关于PostgreSQL" class="headerlink" title="关于PostgreSQL"></a>关于PostgreSQL</h2><p>PostgreSQL 是一款开源数据库管理系统，已经有超过30年的历史。其 <a href="https://www.postgresql.org/" target="_blank" rel="noopener">官方网站</a> 的介绍为：</p>
<blockquote>
<p>PostgreSQL: The World’s Most Advanced Open Source Relational Database</p>
</blockquote>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><p>本安装过程<strong>仅适用于 ubuntu18.04</strong>，其它系统下的安装可以参考 <a href="https://www.postgresql.org/download/" target="_blank" rel="noopener">官方文档</a>。</p>
<h3 id="下载"><a href="#下载" class="headerlink" title="下载"></a>下载</h3><ul>
<li>在<code>/etc/apt/sources.list.d/</code>中新建一个源配置文件<code>pgdg.list</code>，内容如下<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main</span><br></pre></td></tr></table></figure></li>
<li>导入签名文件<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -</span><br></pre></td></tr></table></figure></li>
<li>更新apt<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get update</span><br></pre></td></tr></table></figure>
<h3 id="安装-1"><a href="#安装-1" class="headerlink" title="安装"></a>安装</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install postgresql-11`</span><br></pre></td></tr></table></figure>



</li>
</ul>
<h2 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h2><p>PostgreSQL初次安装后，默认会生成一个名为<code>postgres</code>的数据库和一个名为<code>postgres</code>的数据库用户。同时还生成了一个名为<code>postgres</code>的Linux系统用户。<br>安装完成后，就使用<code>postgres</code>用户创建其它用户和相关的数据库，这里需要使用PostgreSQL的两个命令行工具：<code>createuser</code>和<code>createdb</code>以及控制台程序<code>psql</code>。</p>
<ul>
<li><p>以postgres用户身份创建另一个用户dbuser，并指定其为超级用户，这个时候由于linux用户和数据用户相同，所以不需要输入用户postgres的密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo -u postgres createuser --superuser dbuser</span><br></pre></td></tr></table></figure></li>
<li><p>使用控制台更改用户密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ sudo -u postgres psql</span><br><span class="line">postgres=<span class="comment">#\password dbuser</span></span><br><span class="line">postgres=<span class="comment">#\q</span></span><br></pre></td></tr></table></figure></li>
<li><p>为用户创建数据库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ sudo -u postgres createdb -O dbuser exampledb</span><br></pre></td></tr></table></figure></li>
<li><p>登录数据库</p>
<p>PostgreSQL的控制台程序为<code>psql</code> 。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ psql -U dbuser -d exampledb -h 127.0.0.1 -p 5432</span><br></pre></td></tr></table></figure>
<p>​       登录成功后，命令行提示符为<code>username=#</code> 。可以输入<code>\?</code> 查看命令行帮助，使用<code>\q</code> 退出。常用的还有：</p>
</li>
</ul>
<table>
<thead>
<tr>
<th align="left">命令</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td align="left">\l</td>
<td>列出所有的数据库。按 q 退出。</td>
</tr>
<tr>
<td align="left">\d</td>
<td>列出当前数据库的所有表</td>
</tr>
<tr>
<td align="left">\d  <table_name></td>
<td>列出指定表的结构</td>
</tr>
<tr>
<td align="left">\du</td>
<td>列出所有数据库用户，按 q 退出。</td>
</tr>
<tr>
<td align="left">\h <sql command></td>
<td>查看SQL语句的帮助，按 q 退出帮助。</td>
</tr>
<tr>
<td align="left">\c <database_name></td>
<td>切换到另一个数据库进行操作</td>
</tr>
</tbody></table>
<p><strong><em>以上内容参考自 <a href="http://www.ruanyifeng.com/blog/2013/12/getting_started_with_postgresql.html" target="_blank" rel="noopener">阮一峰的网络日志</a> 以及 <a href="https://www.postgresql.org/download/linux/ubuntu/" target="_blank" rel="noopener">官方文档</a></em></strong></p>

        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>文章作者:</span>
                        <span>Liux</span>
                    </p>
                
                
                
                    <p class="copyright-item">
                        <span>许可方式:</span>
                        <span>Copyright (c) 2019 <a href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank" rel="noopener">CC-BY-NC-4.0</a> LICENSE</span>
                    </p>
                
                

            </section>
        

        <section class="post-nav">
            
            
            <a class="next" rel="next" href="/2020/02/16/MINDSET_THE_NEW_PSYCHOLOGY_OF_SUCCESS/">《终身成长》读书笔记</a>
            
            <a class="back" href="javascript:window.history.back();">退回</a>
        </section>
    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© Liux | Powered by <a href="https://hexo.io" target="_blank">Hexo</a></span>
    </div>
</footer>

    </div>
</body>
</html>
